package com.googlecode.simpleqms

class StructureController {
    def StructureService

    def getList = {
        def nodeId
        if (params.get('node') == 'Root') {
            redirect(controller: 'division', action: 'getList')
        } else if (params.get('node') =~ 'Div_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            redirect(controller: 'project', action: 'getList', params: [divisionId: nodeId])
        } else if (params.get('node') =~ 'Proj_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            redirect(controller: 'employee', action: 'getList', params: [projectId: nodeId])
        }
    }

    def getElement = {
        def nodeId
        if (params.get('node') == 'Root') {
            nodeId = 1
            redirect(controller: 'company', action: 'getElement', params: [companyId: nodeId])
        } else if (params.get('node') =~ 'Div_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            redirect(controller: 'division', action: 'getElement', params: [divisionId: nodeId])
        } else if (params.get('node') =~ 'Proj_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            redirect(controller: 'project', action: 'getElement', params: [projectId: nodeId])
        } else if (params.get('node') =~ 'Emp_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            redirect(controller: 'employee', action: 'getElement', params: [employeeId: nodeId])
        }
    }

    def addElement = {
        def parentNodeId
        if (params.get('node') == 'Root') {
            parentNodeId = 1
            params.put('companyId', parentNodeId)
            redirect(controller: 'division', action: 'addElement', params: params)
        } else if (params.get('node') =~ 'Div_') {
            parentNodeId = StructureService.getRealNodeId(params.get('node'))
            params.put('divisionId', parentNodeId)
            redirect(controller: 'project', action: 'addElement', params: params)
        } else if (params.get('node') =~ 'Proj_') {
            parentNodeId = StructureService.getRealNodeId(params.get('node'))
            params.put('projectId', parentNodeId)
            redirect(controller: 'employee', action: 'addElement', params: params)
        }
    }

    def modElement = {
        def nodeId
        if (params.get('node') =~ 'Div_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            params.put('divisionId', nodeId)
            redirect(controller: 'division', action: 'modElement', params: params)
        } else if (params.get('node') =~ 'Proj_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            params.put('projectId', nodeId)
            redirect(controller: 'project', action: 'modElement', params: params)
        } else if (params.get('node') =~ 'Emp_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            params.put('employeeId', nodeId)
            redirect(controller: 'employee', action: 'modElement', params: params)
        }
    }

    def delElement = {
        def nodeId
        if (params.get('node') =~ 'Div_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            redirect(controller: 'division', action: 'delElement', params: [divisionId: nodeId])
        } else if (params.get('node') =~ 'Proj_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            redirect(controller: 'project', action: 'delElement', params: [projectId: nodeId])
        } else if (params.get('node') =~ 'Emp_') {
            nodeId = StructureService.getRealNodeId(params.get('node'))
            redirect(controller: 'employee', action: 'delElement', params: [employeeId: nodeId])
        }
    }
}